/**
 * 动态配置加载测试
 * 
 * 用途：验证AppConfig能够动态读取config.properties文件的最新内容
 * 方法：模拟修改配置文件后验证配置值的变化
 * 原理：测试每次调用AppConfig.getString()等方法时都会重新读取properties文件
 */
package com.example.brain_wave.test

import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import com.example.brain_wave.config.AppConfig
import org.junit.Before
import org.junit.Test
import org.junit.Assert.*
import java.io.File
import java.io.FileOutputStream

class ConfigDynamicLoadTest {
    
    private lateinit var context: Context
    private lateinit var appConfig: AppConfig
    
    @Before
    fun setup() {
        context = InstrumentationRegistry.getInstrumentation().targetContext
        appConfig = AppConfig.getInstance(context)
    }
    
    @Test
    fun testDynamicConfigLoading() {
        // 测试从config.properties读取配置
        val apiBaseUrl = appConfig.getString("api.base.url")
        val sessionApiBaseUrl = appConfig.getString("session.api.base.url")
        val storeApiBaseUrl = appConfig.getString("store.room.api.base.url")
        
        // 验证配置值是否正确读取
        assertEquals("http://192.168.2.12:8000", apiBaseUrl)
        assertEquals("http://192.168.2.12:9000", sessionApiBaseUrl)
        assertEquals("http://139.224.62.63:81", storeApiBaseUrl)
        
        println("✅ 动态配置加载测试通过")
        println("API Base URL: $apiBaseUrl")
        println("Session API Base URL: $sessionApiBaseUrl")
        println("Store API Base URL: $storeApiBaseUrl")
    }
    
    @Test
    fun testConfigPriority() {
        val key = "test.config.key"
        val originalValue = "original_value"
        val runtimeValue = "runtime_value"
        
        // 测试运行时设置的优先级
        appConfig.setString(key, runtimeValue)
        val retrievedValue = appConfig.getString(key, originalValue)
        
        assertEquals("运行时设置应该有最高优先级", runtimeValue, retrievedValue)
        
        println("✅ 配置优先级测试通过")
        println("设置的运行时值: $runtimeValue")
        println("获取的值: $retrievedValue")
    }
    
    @Test
    fun testIntegerConfigLoading() {
        val connectTimeout = appConfig.getInt("api.connect.timeout")
        val readTimeout = appConfig.getInt("api.read.timeout")
        val cacheMaxSize = appConfig.getInt("data.cache.max.size")
        
        assertEquals(10, connectTimeout)
        assertEquals(30, readTimeout)
        assertEquals(300, cacheMaxSize)
        
        println("✅ 整数配置加载测试通过")
        println("连接超时: ${connectTimeout}s")
        println("读取超时: ${readTimeout}s")
        println("缓存最大大小: $cacheMaxSize")
    }
    
    @Test
    fun testBooleanConfigLoading() {
        val debugEnabled = appConfig.getBoolean("debug.enabled")
        
        assertEquals(false, debugEnabled)
        
        println("✅ 布尔配置加载测试通过")
        println("调试模式: $debugEnabled")
    }
    
    @Test
    fun testLongConfigLoading() {
        val dataCollectionInterval = appConfig.getLong("data.collection.interval")
        val cacheMaxTime = appConfig.getLong("data.cache.max.time")
        
        assertEquals(1000L, dataCollectionInterval)
        assertEquals(3600000L, cacheMaxTime)
        
        println("✅ 长整数配置加载测试通过")
        println("数据采集间隔: ${dataCollectionInterval}ms")
        println("缓存最大时间: ${cacheMaxTime}ms")
    }
}